/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2024/6/12
 * Time: 11:12
 */

#ifndef GLOBAL_H
#define GLOBAL_H

#include <QtCore>
#include <QtSvg>
#include <QtWidgets>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <memory>
#include <queue>
#include <random>
#include <vector>

namespace Upgrade {
    Q_NAMESPACE

    //花色 ♠♥♦♣🃏
    enum class CardSuit
    {
        Unkonw       = 1 << 0,//无        <-> 无主
        HeartsSuit   = 1 << 1,//红桃
        SpadesSuit   = 1 << 2,//黑桃
        DiamondsSuit = 1 << 3,//方片
        ClubsSuit    = 1 << 4,//梅花
        JokerSuit,            //大小王     <-> 公主
    };
    Q_ENUM_NS(CardSuit)
    Q_DECLARE_FLAGS(CardSuits, CardSuit);

    enum class CardRank
    {
        Unkonw,
        Two = 2,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen,
        King,
        Ace,
        SmallJoker,// 小王
        BigJoker,  // 大王
    };

    static QString Suit2String(CardSuit _suit)
    {
        switch(_suit) {
            case CardSuit::HeartsSuit: return "♥"; break;
            case CardSuit::SpadesSuit: return "♠"; break;
            case CardSuit::DiamondsSuit: return "♦"; break;
            case CardSuit::ClubsSuit: return "♣"; break;
            case CardSuit::JokerSuit: return "🃏"; break;
        }
        return "";
    }

    static QString Rank2String(CardRank _rank)
    {
        switch(_rank) {
            case CardRank::Two: return "2"; break;
            case CardRank::Three: return "3"; break;
            case CardRank::Four: return "4"; break;
            case CardRank::Five: return "5"; break;
            case CardRank::Six: return "6"; break;
            case CardRank::Seven: return "7"; break;
            case CardRank::Eight: return "8"; break;
            case CardRank::Nine: return "9"; break;
            case CardRank::Ten: return "10"; break;
            case CardRank::Jack: return "J"; break;
            case CardRank::Queen: return "Q"; break;
            case CardRank::King: return "K"; break;
            case CardRank::Ace: return "A"; break;
            case CardRank::SmallJoker: return "Joker"; break;
            case CardRank::BigJoker: return "Joker"; break;
            default: return "";
        }
    }

    static QString GetSVGName(CardSuit _suit)
    {
        auto    mate = QMetaEnum::fromType<CardSuit>();
        QString name = mate.valueToKey(static_cast<int>(_suit));
        return QString(":/%1").arg(name);
    }

    static QByteArray GetContent(const QString &_name, const QColor &color)
    {
        QFile file(_name);
        file.open(QIODevice::Text | QIODevice::ReadOnly);
        QString ret = file.readAll();
        file.close();

        if(color.isValid()) {
            auto s = QString(R"(fill="%1")").arg(color.name());
            ret    = ret.replace(QRegularExpression(R"(fill="#.*?")"), s.toLocal8Bit());
        }

        return ret.toUtf8();
    }

    static QByteArray GetTextContent(const QString &_text, const QColor & _bgColor,const QColor & _textColor)
    {
        QFile file(":/template/TextTemplate");
        file.open(QIODevice::Text | QIODevice::ReadOnly);
        QString ret = file.readAll();
        file.close();

        Q_ASSERT(_text.size() == 1);
        Q_ASSERT(_bgColor.isValid());
        Q_ASSERT(_textColor.isValid());

        ret = ret.arg(_bgColor.name(),_textColor.name(),_text);

        return ret.toUtf8();
    };

    static QPixmap GetPixmap(const QByteArray &_data, const QSize &_size = QSize())
    {
        QSvgRenderer renderer;
        renderer.load(_data);
        renderer.setAspectRatioMode(Qt::KeepAspectRatio);
        Q_ASSERT(renderer.isValid());

        QPixmap pixmap(_size.isEmpty() ? renderer.viewBox().size() : _size);
        pixmap.fill(Qt::transparent);

        QPainter painter(&pixmap);
        renderer.render(&painter);
        Q_ASSERT(!pixmap.isNull());

        return pixmap;
    }

}// namespace Upgrade

#endif //GLOBAL_H
